<template>
	<view class="container">
		<view v-if="!Object.keys(order).length" class="d-flex w-100 h-100 flex-column just-content-center align-items-center">
			<image src="/static/images/logo.png" class="drinks-img"></image>
			<view class="tips d-flex flex-column align-items-center font-size-base text-color-assist">
				<view>您还没有体验过网红打卡地</view>
				<view>快去犒劳一下自己吧~</view>
			</view>
			<button type="primary" class="drink-btn" size="default" @tap="ticket">去购票</button>
			<!-- <view class="font-size-sm text-color-primary" @tap="orders">查看历史订单</view> -->
		</view>
		<template v-else>
			<view class="order-box">
				<view class="bg-white">
					<view class="section">
						<!-- store info begin -->
						<list-cell :hover="false">
							<view class="w-100 d-flex align-items-center">
								<view class="d-flex flex-column w-60">
									<view class="w-100 font-size-lg text-color-base text-truncate">{{ order.store.storeName }}</view>
								</view>
								<view class="d-flex justify-content-end align-items-center w-40">
									<image src="/static/images/order/mobile.png" style="width: 60rpx; height: 60rpx;margin-right: 30rpx;" @tap="phoneCall(order.store.storePhone)"></image>
									<!-- <image src="/static/images/order/navigation.png" style="width: 60rpx; height: 60rpx;"></image> -->
								</view>
							</view>
						</list-cell>
						<!-- store info end -->
						<list-cell :hover="false" padding="50rpx 30rpx">
							<view class="w-100 d-flex flex-column">
								<view class="d-flex align-items-center just-content-center" >
									<!-- <view class="sort-num">{{ order.sort_num }}</view> -->
									<canvas :style="{ width: qrcodeSize + 'px', height: qrcodeSize + 'px' }"
									 canvas-id="orderQrCode"></canvas>
								</view>
								<!-- steps begin -->
								<view class="d-flex just-content-center mt-30">
									<view class="steps d-flex flex-column w-100">
										
										<view class="steps__text-column">
											<view class="steps__text-column-item" :class="{active: order.status === 0}">
												<view class="steps__column-item-line bg-transparent"></view>
												<view class="steps__text-column-item-text">待付款</view>
												<view class="steps__column-item-line"></view>
											</view>
											<view class="steps__text-column-item" :class="{active: order.status >= 1}">
												<view class="steps__column-item-line bg-transparent"></view>
												<view class="steps__text-column-item-text">已下单</view>
												<view class="steps__column-item-line"></view>
											</view>
											<view class="steps__text-column-item" :class="{active: order.status >= 2}">
												<view class="steps__column-item-line"></view>
												<view class="steps__text-column-item-text">参观中</view>
												<view class="steps__column-item-line"></view>
											</view>
											<view class="steps__text-column-item" :class="{active: order.status >= 3}">
												<view class="steps__column-item-line"></view>
												<view class="steps__text-column-item-text">待评价</view>
												<view class="steps__column-item-line"></view>
											</view>
											<view class="steps__text-column-item" :class="{active: order.status >= 4}">
												<view class="steps__column-item-line"></view>
												<view class="steps__text-column-item-text">
													完成
												</view>
												<view class="steps__column-item-line bg-transparent"></view>
											</view>
										</view>
									</view>
								</view>
								<!-- steps end -->
								<view v-if="order.status === 0" class="d-flex just-content-center align-items-center font-size-base text-color-assist mb-40">
									请将 <text class="text-color-primary mr-10 ml-10">二维码</text> 展示给店员完成付款
								</view>
								<!-- goods begin -->
								<view class="w-100 d-flex flex-column position-relative mt-30" style="margin-bottom: -40rpx;">
									<view class="w-100 d-flex align-items-center mb-40" v-for="(good, index) in order.items" :key="index">
										<view class="d-flex flex-column w-60 overflow-hidden">
											<view class="font-size-lg text-color-base mb-10 text-truncate">{{ good.productName }}</view>
											<view class="font-size-sm text-color-assist text-truncate">{{ good.specContent }}</view>
										</view>
										<view class="d-flex w-40 align-items-center justify-content-between pl-30">
											<view class="font-size-base text-color-base">x{{ good.count }}</view>
											<view class="font-size-base text-color-base font-weight-bold">￥{{ good.salePrice }}</view>
										</view>
									</view>
								</view>
								<!-- goods end -->
							</view>
						</list-cell>
					</view>
					<view class="section">
						<!-- payment and amount begin -->
						<list-cell :hover="false" padding="50rpx 30rpx">
							<view class="w-100 d-flex flex-column">
								<view class="pay-cell">
									<view>支付方式</view>
									<view class="font-weight-bold">{{ getPayTypeStr(order.paymentType) }}</view>
								</view>
								<view class="pay-cell">
									<view>实付金额</view>
									<view class="font-weight-bold">￥{{ order.payPrice }}</view>
								</view>
								<view class="pay-cell">
									<view>金额总计</view>
									<view class="font-weight-bold">￥{{ order.totalPrice }}</view>
								</view>
							</view>
						</list-cell>
						<!-- payment and amount end -->
					</view>
					<view class="section">
						<!-- order info begin -->
						<list-cell :hover="false" padding="50rpx 30rpx">
							<view class="w-100 d-flex flex-column">
								<view class="pay-cell">
									<view>下单时间</view>
									<view class="font-weight-bold">{{ order.createTime}}</view>
								</view>
								<view class="pay-cell">
									<view>下单门店</view>
									<view class="font-weight-bold">{{ order.store.storeName }}</view>
								</view>
								<view class="pay-cell">
									<view>订单号</view>
									<view class="font-weight-bold">{{ order.orderNum }}</view>
								</view>
							</view>
						</list-cell>
						<!-- order info end -->
					</view>
					
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	import listCell from '@/components/list-cell/list-cell'
	const qrCode = require('@/libs/weapp-qrcode.js');
	import {
		mapState,
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex'
	import {
		currentTicketOrder
	} from '@/api/order.js'
	
	export default {
		components: {
			listCell
		},
		data() {
			return {
				order:{},
				qrcodeSize: uni.upx2px(260)
			}
		},
		computed: {
			...mapGetters(['isLogin', 'userInfo', 'currentLocation'])
		},
		onShow: function() {
			if(this.isLogin) {
				this.getCurrentTicketOrder();
			}
		},
		methods: {
			getCurrentTicketOrder() {
				let that = this;
				uni.showLoading({
					title: '加载中'
				})
				currentTicketOrder().then(res => {	
					uni.hideLoading()
					if (res.success) {
						that.order = res.data;
						let params = {
							id:res.data.id,
							type:2//0会员卡使用 1 购卡 2 商品 
						}
						this.createQrCode(JSON.stringify(params))
					}
				}, error => {
					uni.hideLoading()
					console.log(error);
				});
			},
			getPayTypeStr(payType) {
				//0 线下 1 小程序 2 公众号 3 H5 4 PC 5 支付宝 6 余额
				let tmp = "线下支付";
				switch (payType) {
					case 6:
						tmp = "余额支付";
						break;
					case 5:
						tmp = "支付宝支付";
						break;
					case 4:
						tmp = "微信支付";
						break;
					case 3:
						tmp = "微信支付";
						break;
					case 2:
						tmp = "微信支付";
						break;
					case 1:
						tmp = "微信支付";
						break;
					default:
						tmp = "线下支付";
						break;
				}
				return tmp;
			},
			orders() {
				if(!this.isLogin) {
					uni.navigateTo({url: '/pages/login/login'})
					return
				}
				uni.navigateTo({
					url: '/pages/order/index'
				})
			},
			ticket() {
				uni.switchTab({
					url: '/pages/tabbar/ticket/index'
				})
			},
			phoneCall(phone){
				uni.makePhoneCall({
				    phoneNumber: phone
				});
			},
			createQrCode(text) {
				new qrCode('orderQrCode', {
					text: text,
					width: this.qrcodeSize,
					height: this.qrcodeSize,
					colorDark: '#333333',
					colorLight: '#FFFFFF',
					correctLevel: qrCode.CorrectLevel.H
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifdef H5 */
	page {
		min-height: 100%;
		background-color: $bg-color;
	}
	/* #endif */
	.order-box {
		padding: 20rpx;
		/* #ifdef H5 */
		margin-bottom: 100rpx;
		/* #endif */
	}
	
	.drinks-img {
		width: 260rpx;
		height: 260rpx;
	}
	
	.tips {
		margin: 60rpx 0 80rpx;
		line-height: 48rpx;
	}
	
	.drink-btn {
		width: 320rpx;
		border-radius: 50rem !important;
		margin-bottom: 40rpx;
		font-size: $font-size-base;
		line-height: 3.0;
	}
	
	@mixin arch {
		content: "";
		position: absolute;
		background-color: $bg-color;
		width: 30rpx;
		height: 30rpx;
		bottom: -15rpx;
		z-index: 10;
		border-radius: 100%;
	}
	
	.section {
		position: relative;
		
		&::before {
			@include arch;
			left: -15rpx;
		}
		
		&::after {
			@include arch;
			right: -15rpx;
		}
	}
	
	.pay-cell {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: $font-size-base;
		color: $text-color-base;
		margin-bottom: 40rpx;

		&:nth-last-child(1) {
			margin-bottom: 0;
		}
	}
	
	.sort-num {
		font-size: 64rpx;
		font-weight: bold;
		color: $text-color-base;
		line-height: 2;
	}
	
	.steps__img-column {
		display: flex;
		margin: 30rpx 0;
		
		.steps__img-column-item {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			
			image {
				width: 80rpx;
				height: 80rpx;
			}
		}
	}
	
	.steps__text-column {
		display: flex;
		margin-bottom: 40rpx;
		
		.steps__text-column-item {
			flex: 1;
			display: inline-flex;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: $font-size-base;
			color: $text-color-assist;
			
			&.active {
				color: $text-color-base;
				font-weight: bold;
				
				.steps__column-item-line {
					background-color: $text-color-base;
				}
			}
			
			.steps__column-item-line{
				flex: 1;
				height: 2rpx;
				background-color: #919293;
				transform: scaleY(0.5);
			}
			
			.steps__text-column-item-text {
				margin: 0 8px;
			}
		}
	}
</style>
